package com.rui1024.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        HttpSession session = req.getSession();
        if(session.getAttribute("currentUser")==null && !req.getRequestURI().contains("login")&& !req.getRequestURI().contains("static")
                && !req.getRequestURI().contains("css")&& !req.getRequestURI().contains("img")&& !req.getRequestURI().contains("js")
                && !req.getRequestURI().contains("register.jsp")){
            resp.sendRedirect("login.jsp");
            return;
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
        System.out.println("销毁...");
    }
}
